<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2023/2/15
  Time: 9:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="header.jsp"%>
</head>
<body>
    <form id="formId" class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户头像</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1" width="250px" height="100px">
                        <input type="hidden" name="avatar" id="image"/>
                        <p id="demoText"></p>
                    </div>
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <button type="button" onclick="submitForm()" class="layui-btn" lay-submit="" lay-filter="demo2">添加</button>
        </div>
    </form>

    <script>
        layui.use(['form','upload'], function() {
            var form = layui.form;
            var upload = layui.upload;

            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: '/upload/uploadImage' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });

                }
                ,done: function(jsonResult){
                    //上传成功的一些操作
                    //……
                    $('#image').val(jsonResult.data);
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });
        });

        function submitForm() {
            $.post(
                '/user/add',
                $('#formId').serialize(), // {title:'Java面对对象',typeId:1,content:'dd'}
                function(jsonResult) {//回调函数
                    console.log(jsonResult);
                    if (jsonResult.ok) {
                        mylayer.okMsg('添加成功');
                        // 获得当前弹出框的index
                        var index = parent.layer.getFrameIndex(window.name);
                        // 关闭弹出层
                        parent.layer.close(index);
                        // 刷新父页面
                        window.parent.location.reload();
                    } else {
                        mylayer.errorMsg('添加失败');
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>
